#include "Input.h"
#include <iostream>

InputPtr Input::singleton;

InputPtr Input::getSingleton()
{
    if(singleton == nullptr)
        singleton = std::make_shared<Input>();
    return singleton;
}

Input::Input()
{
}

Input::~Input()
{
	if(thread.joinable())
		thread.join();
}

void Input::init()
{
    thread = std::thread(&Input::Main, this);
}

void Input::Main()
{
	join = false;
    block = false;
	while(true)
	{
		if(join == true)
			break;

		if(ready == false && block == false)
		{
			std::getline(std::cin, line);
            if(block == false)
			    ready = true;
		}
	}
	std::cout << "Exit Input Loop" << std::endl;
}

void Input::Join()
{
	join = true;
}

const std::string &Input::getLine()
{
	if(ready == true) 
	{
		output = line;
		ready = false;
		return output; 
	}
	else 
		return empty_line;
}
